import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Unigram {

	public Map<String, Integer> count = new HashMap<String, Integer>();

	public static void main(String[] args) {
		Unigram u = new Unigram("test.txt");
	}

	public Unigram(String file) {
		makeTable(file);
	}

	private void makeTable(String file) {
		try {
			FileReader fread = new FileReader(file);
			BufferedReader br = new BufferedReader(fread);
			String strLine;

			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				String[] array = strLine.split("\\s+");
				for (int i = 0; i < array.length; i++) {
					String target = array[i].toLowerCase(); // original word
					ArrayList<String> targetlist = breakUpPunctuation(target); // break up into list, punct is own word
					for (int j = 0; j < targetlist.size(); j++) { // for each new word
						target = targetlist.get(j);
						if (count.containsKey(target)) {
							count.put(target, count.get(target) + 1);

						} else {
							count.put(target, 1);
						}
						System.out.print(target);
						System.out.println(count.get(target));
					}
				}
			}
			// Close the input stream
			br.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	private static ArrayList<String> breakUpPunctuation(String target) {
		ArrayList<String> output = new ArrayList<String>();
		String prev = "";
		// We want to avoid treating HTML tags as words
		if (target.length() > 0 && target.charAt(0) == '<') {
			return output;
		}
		for (int ptr = 0; ptr < target.length(); ptr++) {
			if (target.charAt(ptr) == '.' || target.charAt(ptr) == '!' || target.charAt(ptr) == '?'
					|| target.charAt(ptr) == ':' || target.charAt(ptr) == ';') {
				if (prev.length() > 0) {
					output.add(prev);
					prev = "";
				}
				output.add(target.substring(ptr, ptr + 1));
			} else if (target.charAt(ptr) != '(' && target.charAt(ptr) != ')' && target.charAt(ptr) != '\''
					&& target.charAt(ptr) != '\"' && target.charAt(ptr) != ',') {
				prev = prev + target.substring(ptr, ptr + 1);
			}
		}
		if (prev.length() > 0) {
			output.add(prev);
			prev = "";
		}
		return output;
	}

}
